//
//  TransactionRecord.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/5.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "TransactionRecord.h"
#import "MineServer.h"
#import "XSelectWalletListView.h"
#import "ImportTheWalletViewController.h"
#import "TransactionViewController.h"
@interface TransactionRecordCell : XHBaseTableCell
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UILabel *subLab;
@property (nonatomic,strong) UILabel *detailLab;

@end
@implementation TransactionRecordCell

- (void)setupUI{
    self.titleLab = [UILabel new];
    self.titleLab.textColor = UIColorHex(#333333);
    self.titleLab.font = kDefaultFont(15);
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.top.mas_equalTo(AdaptedWidth(20));
    }];
    self.subLab = [UILabel new];
    self.subLab.textColor = UIColorHex(#B5B5B5);
    self.subLab.font = kDefaultFont(12);
    [self.contentView addSubview:self.subLab];
    [self.subLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.bottom.mas_equalTo(AdaptedWidth(-5));
    }];
    self.detailLab = [UILabel new];
    self.detailLab.textColor = UIColorHex(#333333);
    self.detailLab.font = kBoldFont(16);
    [self.contentView addSubview:self.detailLab];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLab.mas_centerY);
        make.right.mas_equalTo(-kNormalSpace);
    }];
}


@end

@interface TransactionRecord ()
@property (nonatomic,strong) MineServer *mineServer;
@property (nonatomic,strong) Wallet *seletWallet;
@end

@implementation TransactionRecord

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.seletWallet = CURRENT_WALLET;
    [self.tableView.mj_header beginRefreshing];
}

- (void)setupUI{
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = AdaptedWidth(60);
    [self addNavigationItemWithImageNames:@[@"black_wallet"] isLeft:NO target:self action:@selector(rightBtnClick) tags:nil];
}
- (void)rightBtnClick{
    kWeakSelf(self);
    [XSelectWalletListView showList:[[XHDataBaseManager shareInstance]selectAllLocalWallet] selectWallet:self.seletWallet selectAction:^(Wallet * _Nonnull selectWallet) {
        if (![weakself.seletWallet.wallet_uid isEqualToString:selectWallet.wallet_uid]) {
            weakself.seletWallet = selectWallet;
            [weakself.mineServer.transcationList removeAllObjects];
            [weakself.tableView reloadData];
            [weakself loadMore:NO];
        }
    } cancelAction:^{
        
    } importAction:^{
        ImportTheWalletViewController *vc = [ImportTheWalletViewController new];
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)loadMore:(BOOL)isMore{
    self.mineServer.account = self.seletWallet.wallet_name;
    [self.mineServer getActionListIsMore:isMore completion:^(BOOL success, BOOL isHaveNextPage, NSString * _Nonnull msg) {
        [self endHeaderFooterRefreshingIsHaveNextPage:isHaveNextPage];
        if (success) {
            [self.tableView reloadData];
        }else{
            [self.view makeToast:msg];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineServer.transcationList.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TransactionRecordCell *cell =[TransactionRecordCell cellForTableView:tableView];
    TranscationInfoM *info = [self.mineServer.transcationList objectAtIndex:indexPath.row].data;
    TranscationModel *model = [self.mineServer.transcationList objectAtIndex:indexPath.row];
    cell.titleLab.text = [info.from isEqualToString:self.mineServer.account]?info.to:info.from;
    cell.subLab.text = model.block_time;
    cell.detailLab.text = [NSString stringWithFormat:@"%@%@",[info.from isEqualToString:self.mineServer.account]?@"-":@"+",info.quantity];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TransactionViewController *vc = [TransactionViewController new];
    vc.transcationModel = [self.mineServer.transcationList objectAtIndex:indexPath.row];
    vc.curWalletName = self.seletWallet.wallet_name;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - getter
- (MineServer*)mineServer{
    if (!_mineServer) {
        _mineServer = [MineServer new];
    }
    return _mineServer;
}
@end
